package ihm;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

import java.util.Vector;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import dao.DAO_Client;

import metier.Client;
import metier.TypeVelo;
import metier.Velo;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class FenetreModifierVelo extends JInternalFrame implements ActionListener, ItemListener 
{
	private JButton button;
	private JComboBox cBoxTypeVelo;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JTextField txtQuantite;
	private JButton btnModifier;
	private JButton btnAnnuler;
	private JComboBox<Velo> cBoxVelo;
	private JPanel panel;
	private Vector<Velo> lesVelos;
	private Vector<TypeVelo> lesTypeVelos;
	private JTextField txtMarque;

	public FenetreModifierVelo(Vector<Velo> lesVelos, Vector<TypeVelo> lesTypeVelos)
	{
		setRootPaneCheckingEnabled(false);
		javax.swing.plaf.InternalFrameUI ui = this.getUI();
		((javax.swing.plaf.basic.BasicInternalFrameUI)ui).setNorthPane(null);
		setBorder(null);
		
		this.lesVelos = lesVelos;
		this.lesTypeVelos = lesTypeVelos;
		
		setBounds(100, 100, 710, 440);
		getContentPane().setLayout(null);
		
		button = new JButton("...");
		button.setBounds(578, 116, 30, 23);
		getContentPane().add(button);
		
		cBoxTypeVelo = new JComboBox(lesTypeVelos);
		cBoxTypeVelo.addItemListener(this);
		cBoxTypeVelo.setBounds(368, 117, 200, 20);
		getContentPane().add(cBoxTypeVelo);		
		
		label = new JLabel("Marque :");
		label.setBounds(88, 157, 69, 14);
		getContentPane().add(label);
		
		label_1 = new JLabel("Type de v\u00E9lo :");
		label_1.setBounds(289, 120, 128, 14);
		getContentPane().add(label_1);
		
		label_2 = new JLabel("Quantit\u00E9 :");
		label_2.setBounds(289, 160, 95, 14);
		getContentPane().add(label_2);
		
		txtMarque = new JTextField();
		txtMarque.setColumns(10);
		txtMarque.setBounds(167, 154, 112, 20);
		getContentPane().add(txtMarque);
		
		txtQuantite = new JTextField();
		txtQuantite.setColumns(10);
		txtQuantite.setBounds(368, 157, 112, 20);
		getContentPane().add(txtQuantite);
		
		btnModifier = new JButton("Modifier");
		btnModifier.addActionListener(this);
		btnModifier.setBounds(519, 202, 89, 23);
		getContentPane().add(btnModifier);
		
		btnAnnuler = new JButton("Annuler");
		btnAnnuler.setBounds(420, 202, 89, 23);
		getContentPane().add(btnAnnuler);
		
		cBoxVelo = new JComboBox(lesVelos);
		cBoxVelo.addItemListener(this);
		cBoxVelo.setBounds(88, 116, 191, 22);
		getContentPane().add(cBoxVelo);
		
		panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Modifier un v\u00E9lo", TitledBorder.LEADING, TitledBorder.TOP, new Font("Tahoma", Font.BOLD, 13), null));
		panel.setBounds(57, 75, 587, 175);
		getContentPane().add(panel);
		chargementVelo();
	}
	
	public void actionPerformed(ActionEvent evt) {
		// Bouton Annuler
		if(evt.getSource() == btnAnnuler)
		{
            this.dispose();
		}	
		
		if(evt.getSource() == btnModifier)
		{
			Velo cBxVelo;
			cBxVelo = (Velo)cBoxVelo.getSelectedItem();
			TypeVelo cBxTypeVelo;
			cBxTypeVelo = (TypeVelo)cBoxTypeVelo.getSelectedItem();
		}
	}
	
	public void itemStateChanged(ItemEvent evt) 
	{
		chargementVelo();
	}
	
	public void chargementVelo()
	{
		Velo item;
		item = (Velo)cBoxVelo.getSelectedItem();
		txtMarque.setText(item.getLibelleVelo());
		txtQuantite.setText(String.valueOf(item.getQuantite()));
		cBoxTypeVelo.setSelectedItem(item.getUnType().getIdTypeVelo());
	}
}
